package com.iot.inter.controller.newinterface.trafficusage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.response.Page;
import com.iot.inter.entity.newpojo.response.subResponse.SingleDayTrafficUsageResponse;
import com.iot.inter.entity.newpojo.response.subResponse.common.DeviceFlowList;
import com.iot.inter.entity.newrequest.trafficusage.singleday.request.RequestSingleDayNew;
import com.iot.inter.entity.newrequest.trafficusage.singleday.response.DeviceFlow;
import com.iot.inter.entity.newrequest.trafficusage.singleday.response.ResponseSingleDayNew;
import com.iot.inter.interfaceprocessing.newinterface.TurnTrafficUsageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 去北京做：3.7	查询单日流量使用情况:2.0接口
 * <p>
 * 接入方可以通过调用该接口，能够批量查询某天所有设备的流量使用情况。不支持请求当天的流量使用情况查询。
 * 考虑到拥有的设备数量会很多，所以本接口按照分页形式，每次查询仅获得一页设备流量使用信息，其中每页包含查询结果数量，由接入方自行定义。
 * 若要获取所有设备的流量使用情况，则需要多次调用该接口。
 *
 * @author lm
 * @date 2019/8/5
 */
@RestController
@Api(description = "3.7 查询单日流量使用情况")
public class TrafficUsageQuerySingleDayController {

    private static Logger logger = Logger.getLogger(TrafficUsageQuerySingleDayController.class);

    @CrossOrigin
    @RequestMapping(value = "singleDayTrafficUsage", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.7 查询单日流量使用情况", response = ResponseSingleDayNew.class)
    public String singleDayTrafficUsage(@ApiParam @RequestBody RequestSingleDayNew request) {

        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);
        ResponseSingleDayNew response = new ResponseSingleDayNew();

        if (requestStr != null) {

            logger.info("【查询单日流量使用情况】请求：" + requestStr);

            RequestSingleDayNew requestPackage = JSONArray.parseObject(requestStr, RequestSingleDayNew.class);
            Integer pageNo = requestPackage.getPageNo();
            Integer pageSize = requestPackage.getPageSize();
            String date = requestPackage.getDate();
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();

            TurnTrafficUsageService trafficUsageService = new TurnTrafficUsageService();
            SingleDayTrafficUsageResponse result = trafficUsageService.singleDay(pageNo, pageSize, date, secret, partnerCode);


            if (result != null && !result.equals("-1")) {

                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(result.getHeader().getResultCode())) {

                    if (result.getBody().getDevice_flow() != null) {

                        List<DeviceFlowList> device_flow = result.getBody().getDevice_flow();

                        List<DeviceFlow> deviceFlowList = new ArrayList<DeviceFlow>();

                        for (DeviceFlowList flowList : device_flow) {
                            DeviceFlow df = new DeviceFlow();
                            df.setDeviceId(flowList.getDeviceId());
                            df.setQuerydayFlow(flowList.getQuerydayFlow());
                            deviceFlowList.add(df);
                        }

                        Page page = new Page();
                        page.setCurrentPageNo(result.getBody().getPage().getCurrentPageNo());
                        page.setPageSize(result.getBody().getPage().getPageSize());
                        page.setTotalCount(result.getBody().getPage().getTotalCount());
                        page.setTotalPages(result.getBody().getPage().getTotalPages());

                        response.setDeviceFlow(deviceFlowList);
                        response.setDeviceFlowListNum(Integer.parseInt(result.getBody().getDeviceFlowListNum()));
                        response.setRetCode(result.getHeader().getResultCode());
                        response.setResultInfo(result.getHeader().getResultInfo());
                        response.setPage(page);

                        // 返回给调用者的报文
                        jsonString = JSON.toJSONString(response);
                        logger.info("【查询单日流量使用情况】成功响应：" + jsonString);
                        return jsonString;

                    } else {

                        response.setRetCode(result.getHeader().getResultCode());
                        response.setResultInfo(result.getHeader().getResultInfo());
                        jsonString = JSON.toJSONString(response);
                        logger.info("【查询单日流量使用情况】响应：" + jsonString);
                        return jsonString;


                    }

                } else {

                    response.setRetCode(result.getHeader().getResultCode());
                    response.setResultInfo(result.getHeader().getResultInfo());
                    jsonString = JSON.toJSONString(response);
                    logger.info("【查询单日流量使用情况】响应报文：" + jsonString);
                    return jsonString;

                }

            } else {

                response.setRetCode("9998");
                response.setResultInfo("响应空");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(response);
                logger.info("【查询单日流量使用情况】响应：" + jsonString);
                return jsonString;

            }


        } else {

            response.setRetCode("9998");
            response.setResultInfo("请求空");
            jsonString = JSON.toJSONString(response);
            logger.info("【查询单日流量使用情况】响应：" + jsonString);
            return jsonString;

        }

    }


}
